package com.github.chirspan.xaas.uc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.chirspan.xaas.uc.dao.OrgAffiliationDao;
import com.github.chirspan.xaas.uc.dao.OrgAffiliationMemberDao;
import com.github.chirspan.xaas.uc.model.OrgAffiliation;
import com.github.chirspan.xaas.uc.model.OrgAffiliationMember;
import com.github.chirspan.xaas.mybatis.service.impl.BaseServiceImpl;
import com.github.chirspan.xaas.mybatis.util.tree.TreeUtils;
import com.github.chirspan.xaas.uc.service.IOrgAffiliationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: chenpan
 * @Date: 2021-08-10 11:03:09
 * @Description: 联盟信息 接口实现
 */
@Service
public class OrgAffiliationServiceImpl extends BaseServiceImpl<OrgAffiliationDao, OrgAffiliation> implements IOrgAffiliationService {

    @Autowired
    private OrgAffiliationDao affiliationDao;

    @Autowired
    private OrgAffiliationMemberDao affiliationMemberDao;

    @Override
    public List<OrgAffiliation> findAffTree(OrgAffiliation affiliation) {
        List<OrgAffiliation> affiliations = super.selectAll();
        return TreeUtils.buildTree(affiliations);
    }

    @Override
    public boolean delete(String id) {
        if (StrUtil.isEmpty(id)) {
            return false;
        }
        List<String> ids = StrUtil.split(id, ',');
        if (CollectionUtil.isEmpty(ids)) {
            return false;
        }
        for (String affId : ids) {
            affiliationDao.deleteById(affId);
            LambdaQueryWrapper<OrgAffiliationMember> queryWrapper = Wrappers.lambdaQuery(OrgAffiliationMember.class);
            queryWrapper.eq(OrgAffiliationMember::getAffiliationId, affId);
            affiliationMemberDao.delete(queryWrapper);
        }
        return true;
    }
}
